package com.xyzboom.compiler.util.gui

import mu.KotlinLogging
import java.awt.Dimension
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.awt.image.BufferedImage
import javax.swing.*
import javax.swing.SwingConstants.LEADING

private val logger = KotlinLogging.logger { }


class ImageWindow(image: BufferedImage) : JFrame() {
    var closeCallback: () -> Unit = {
        logger.trace { "ori event" }
    }

    init {
        val label = JLabel("", ImageIcon(image), LEADING).apply {
            minimumSize = Dimension(600, 400)
        }
        val scrollPane = JScrollPane(
            label,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
        )
        contentPane = scrollPane
        pack()
        addWindowListener(object : WindowAdapter() {
            override fun windowClosing(e: WindowEvent?) {
                super.windowClosing(e)
                closeCallback()
            }
        })
    }

}